import Taro, {Component} from '@tarojs/taro'
import {View, Input, Button} from '@tarojs/components'
import {connect} from '@tarojs/redux'
import Head from '../../components/head/head'
import {accessUserToken} from '../../actions/user'

import './login.scss'

@connect((store)=>{
  return {user: store.user}
},(dispatch) => {
  return {
    accessUserToken(params) {
      return dispatch(accessUserToken(params))
    }
  }
})

class Template extends Component {
  config = {
    navigationBarTitleText: '登录'
  }
  state = {

  }
  changeText (e) {
    if(e.target) {
      this.setState({
        token: e.target.value
      })
    }
  }
  async loginToken () {
    let {accessUserToken} = this.props
    if(this.state.token) {
      if(await accessUserToken({accesstoken: this.state.token})) {
        console.log('success');
        Taro.redirectTo({url: '/pages/user/user'})
      }
      // if(accessUserToken) {
      //   accessUserToken({accesstoken: this.state.token}).then(res => {
      //     if(res) {
      //       console.log('success');
      //       Taro.redirectTo({url: '/pages/user/user'})
      //     }
      //   })
      // }
    }else{
      Taro.showToast({title: '请输入accesstoken', icon: 'none'})
    }
  }
  render () {
    return (
      <View className='login-wrap'>
        <Head />
        <View className='login-form'>
          <Input className='login-input' onInput={this.changeText.bind(this)} placeholder='请输入accesstoken' />
          <Button onClick={this.loginToken.bind(this)} className='login-btn'>login</Button>
        </View>
      </View>
    )
  }
}

export default Template